package com.jwgf.smarowindingmachine_unity.logic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.jwgf.common.AppContext;
import com.jwgf.common.Utility;
import com.jwgf.fram.FramManager;
import com.jwgf.group.GroupMap;
import com.jwgf.smarowindingmachine_unity.ui.CommonVar;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;

public class MachineParameterSharedPreference {

    public static final String SPINDLE_INFO_IN_PRODATA = "spindleInfoInProData";
    public static final String SPINDLE_INFO_IN_STATICTIME = "spindleInfoInStaticTime";

    public static final String SPINDLE_COUNT = "spindle_count";

    public static final String SYSTEM_LANGUAGE = "system_language";
    public static final String SYSTEM_COUNT_UNIT = "system_count_unit";
    public static final String SYSTEM_LENGTH_UNIT = "system_length_unit";
    public static final String SYSTEM_WEIGHT_UNIT = "system_weight_unit";
    public static final String SYSTEM_MACHINE_DRIVE = "system_machine_drive";
    public static final String SYSTEM_YARN_CLEANER = "system_yarn_cleaner";
    public static final String SYSTEM_MACHINE_NUM = "system_machine_num";
    public static final String SYSTEM_SERIAL_NUM = "system_serial_num";
    public static final String SYSTEM_MACHINE_UNLOCK = "system_machine_unlock";
    public static final String PLC_TYPE = "plc_type";
    public static final String STOP_TYPE = "stop_type";

    public static final String MACHINE_TRANSPORT = "machine_transport";
    public static final String MACHINE_MACHINE_TYPE = "machine_machine_type";//机器类型
    public static final String MACHINE_TRANS_TYPE = "machine_trans_type";
    public static final String MACHINE_SPINNINGIN_TYPE = "machine_spinningin_type";
    public static final String MACHINE_DIRECT_CURRENT = "machine_direct_current";

    public static final String MACHINE_TIME = "machine_time";
    public static final String MACHINE_TIMES = "machine_times";
    public static final String MACHINE_INTERVAL_TIME = "machine_interval_time";
    public static final String MACHINE_CYCLE_NUM = "machine_cycle_num";
    public static final String MACHINE_MINUS_PRESSURE = "machine_minus_pressure";
    public static final String MACHINE_PULL_FORK_OFF_TIME = "machine_fork_off_time";
    public static final String MACHINE_PULL_FORK_ON_TIME = "machine_fork_on_time";
    public static final String MACHINE_EMPTY_TUBE_BELT_TRANSPORT_TYPE = "machine_empty_tube_belt_transport_type";
    public static final String MACHINE_EMPTY_TUBE_BELT_ACTION_INTERVAL = "machine_empty_tube_belt_action_interval";
    public static final String MACHINE_FLAT_BELT_SENSOR = "machine_flat_belt_sensor";

    public static final String FORFEX_TIME = "D0407";
    public static final String OPERATION_TIME = "D0403";
    public static final String DRAUGHT_OPEN_TIME = "D0404";
    public static final String BELLOWS_DROP_TIME = "D0406";
    public static final String THRUM_SUCK_NUM = "D0405";
    public static final String COP_VENT_NUM = "D0408";
    public static final String TRANSMIT_TIME = "D0412";
    public static final String PRESSER_BAR_MOTION = "D0402";
    public static final String MOTOR_DIRECTION = "D0400";
    public static final String FULL_PACKAGE_HANDLE = "D0401";
    public static final String CATEGORY = "D0409";
    public static final String FAILED_CHANNEL_FULL_PROCESS = "159";
/*	public static final String PRESSER_BAR_MOTION_BTN = "presser_bar_motion_btn";
	public static final String MOTOR_DIRECTION_BTN = "motor_direction_btn";
	public static final String FULL_PACKAGE_HANDLE_BTN = "full_package_handle_btn";
	public static final String CATEGORY_BTN	= "category_btn";*/

    public static final String INITIAL_POSITION = "D0211";
    public static final String END_NUM = "D0214";
    public static final String MOTOR_TIME = "D0226";
    public static final String MOVING_SPEED = "D0213";
    public static final String DEBUG_MODE = "D0216";

    public static final String IN_SEGMENT_WAX_DEVICE_HAS = "in_segment_wax_device_has";//0:上蜡装置无    1：上蜡装置有
    public static final String IN_SEGMENT_MOVE_DEVICE_HAS = "in_segment_move_device_has";
    public static final String IN_SEGMENT_DIAMETER = "in_segment_diameter";
    public static final String IN_SEGMENT_TRAVERSE = "in_segment_traverse";
    //	public static final String IN_SEGMENT_ANGLE1 = "in_segment_splicer_stop_angle1";
//	public static final String IN_SEGMENT_ANGLE2 = "in_segment_splicer_stop_angle2";
    public static final String IN_SEGMENT_TIME1 = "in_segment_splicer_stop_time1";
    public static final String IN_SEGMENT_TIME2 = "in_segment_splicer_stop_time2";
    //	public static final String IN_SEGMENT_HELIX = "in_segment_helix_num";
    public static final String IN_SEGMENT_CONICITY = "in_segment_conicity";

    public static final String SPINDLE_ENDS_NUM = "spindle_ends_num";
    public static final String SPINDLE_YARN_CODE = "spindle_yarn_code";
    public static final String SPINDLE_SLIP = "spindle_slip";//滑移系数
    public static final String SPINDLE_PCF = "spindle_pcf";//产量修正系数
    public static final String SPINDLE_COP_NUM = "spindle_cop_num";
    public static final String SPINDLE_SINGLE_STRENGTH = "spindle_single_strenth";//单纱强力
    public static final String SPINDLE_PKG_WEI = "spindle_pkg_wei";
    public static final String SPINDLE_COP_WEI = "spindle_cop_wei";
    public static final String SPINDLE_SAMPLE_YARN_NUM = "spindle_sample_yarn_num";
    public static final String SPINDLE_SAMPLE_UP_LMT = "spindle_sample_up_lmt";
    public static final String SPINDLE_SAMPLE_LOW_LMT = "spindle_sample_low_lmt";
    public static final String SPINDLE_TENS_YARN_NUM = "spindle_tens_yarn_num";
    public static final String SPINDLE_TENS_SENSOR_HAS = "spindle_tens_sensor_has";//0：张力传感器无（开环）  1：张力传感器有（闭环）
    public static final String SPINDLE_ANTI_PTN_MODE = "spindle_anti_ptn_mode";
    public static final String SPINDLE_ANTI_WRAP_SENSE = "spindle_anti_wrap_sense";
    public static final String SPINDLE_PKG_LEN = "spindle_pkg_len";//筒纱长度
    public static final String YARN_NAME = "yarn_name";

    public static final String CBF_ALARM = "cbf_alarm";
    public static final String BAD_SPINDLE = "bad_spindel";

    public static final String DOFFER_START_NUM = "doffer_start_num";
    public static final String DOFFER_RUN_STATUS = "doffer_run_status";
    public static final String CURRENT_SPINDLE_NUMBER = "current_spindle_number";
    private static MachineParameterSharedPreference instance = new MachineParameterSharedPreference();

    private static SharedPreferences sp = AppContext.getInstance().getSharedPreferences("MachineParameter",
            Context.MODE_PRIVATE);

    SharedPreferences shiftsp = AppContext.getInstance().getSharedPreferences("shift_settings",
            Context.MODE_PRIVATE);

    FramManager fm = FramManager.getInstance();

    private MachineParameterSharedPreference() {

    }

    public static MachineParameterSharedPreference getInstance() {
        return instance;
    }

    public void setValue(int index, byte[] request) {
        String mainKey = "" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        int offset = 4;
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            int type = typeList.get(i);
            switch (type) {
                case Byte.SIZE: {
                    String value = String.valueOf(request[offset]);
                    setValue(index, i, value);
                    offset += 1;
                }
                break;
                case Short.SIZE: {
                    String value = String.valueOf(((request[offset + 1] & 0xff) << 8) | request[offset] & 0xff);
                    setValue(index, i, value);
                    offset += 2;
                }
                break;
                case Integer.SIZE: {
                    String value = String.valueOf(((request[offset] & 0xff) << 24) | ((request[offset + 1] & 0xff) << 16)
                            | ((request[offset + 2] & 0xff) << 8) | (request[offset + 3] & 0xff));
                    setValue(index, i, value);
                    offset += 4;
                }
                break;
            }
        }
    }

    public void setValue(int group, int index, byte[] request) {

        String mainKey = group + "-" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        int offset = 4;
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            int type = typeList.get(i);
            switch (type) {
                case Byte.SIZE: {
                    String value = String.valueOf(request[offset]);
                    setValue(group, index, i, value);
                    offset += 1;
                }
                break;
                case Short.SIZE: {
                    String value = String.valueOf(((request[offset + 1] & 0xff) << 8) | request[offset] & 0xff);
                    setValue(group, index, i, value);
                    offset += 2;
                }
                break;
                case Integer.SIZE: {
                    String value = String.valueOf(((request[offset] & 0xff) << 24) | ((request[offset + 1] & 0xff) << 16)
                            | ((request[offset + 2] & 0xff) << 8) | (request[offset + 3] & 0xff));
                    setValue(group, index, i, value);
                    offset += 4;
                }
                break;
            }
        }

    }

    public void setValueNoPulse(int group, int index, byte[] request) {
        String mainKey = group + "-" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        int offset = 4;
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            int type = typeList.get(i);
            switch (type) {
                case Byte.SIZE: {
                    String value = String.valueOf(request[offset]);
                    setValue(group, index, i, value);
                    offset += 1;
                }
                break;
                case Short.SIZE: {

                    if (!((index == 0x24) && (i == 0))) {
                        String value = String.valueOf(((request[offset + 1] & 0xff) << 8) | request[offset] & 0xff);
                        setValue(group, index, i, value);
                    }
                    offset += 2;
                }
                break;
                case Integer.SIZE: {
                    String value = String.valueOf(((request[offset] & 0xff) << 24) | ((request[offset + 1] & 0xff) << 16)
                            | ((request[offset + 2] & 0xff) << 8) | (request[offset + 3] & 0xff));
                    setValue(group, index, i, value);
                    offset += 4;
                }
                break;
            }
        }
    }

    public void setValue(int index, int subIndex, String value) {
        sp.edit().putString(index + "-" + subIndex, value).commit();
    }

    public void setValue(int group, int index, int subIndex, String value) {
        sp.edit().putString(group + "-" + index + "-" + subIndex, value).commit();
    }

    public void setValue(int group, String key, String value) {
        sp.edit().putString(group + "-" + key, value).commit();
    }

    public void setValue(String key, String value) {
        sp.edit().putString(key, value).commit();
    }

    //group  rulemap
    public String getValue(int group, String key) {
        String defValue = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getDefaultValue();
        return sp.getString(group + "-" + key, defValue);
    }

    //rulemap
    public String getValue(String key) {
        String def = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getDefaultValue();
        return getValue(key, def);
    }

    //group
    public String getValue(int group, String key, String def) {
        return sp.getString(group + "-" + key, def);
    }

    public String getValue(String key, String def) {
        return sp.getString(key, def);
    }

    //all  cmd
    public String getValue(int index, int subIndex) {
        String key = index + "-" + subIndex;
        String defValue;
        if (SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key) == null
                || SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getDefaultValue() == null) {
            defValue = "0";
        } else {
            defValue = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getDefaultValue();
        }
        return sp.getString(index + "-" + subIndex, defValue);
    }

    //group cmd  rulemap
    public String getValue(int group, int index, int subIndex) {
        String key = index + "-" + subIndex;
        String defValue;
        if (SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key) == null
                || SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getDefaultValue() == null) {
            defValue = "0";
        } else {
            defValue = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key).getDefaultValue();
        }
        return sp.getString(group + "-" + index + "-" + subIndex, defValue);
    }

    public ArrayList<byte[]> createCanCmdNoGroup(int index, int subIndex, String value) {
        String mainKey = "" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        // cmd is just contain index
        byte[] cmd = Utility.ByteArrayUtils.short2ByteArray((short) ((index << 8) | 0x01));
        // after loop cmd is concated with value
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            String key1 = index + "-" + i;
            int type = typeList.get(i);
            String defv = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key1).getDefaultValue();
            int subValue = Integer.valueOf(sp.getString(key, defv));
            switch (type) {
                case Byte.SIZE: {
                    if (i != subIndex) {
                        byte[] data = new byte[]{(byte) (subValue & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = new byte[]{(byte) (Integer.valueOf(value) & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Short.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray((short) subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray(Short.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Integer.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(Integer.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
            }
        }
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();
        int groupCount = GroupMap.getInstance().getGroupCount();
        for (int group = 1; group <= groupCount; ++group) {
            List<Integer> spindleList = GroupMap.getInstance().getSpindleList(group);
            for (int spindle : spindleList) {
                byte[] id = Utility.ByteArrayUtils.short2ByteArray((short) (spindle + 1));
                cmdList.add(Utility.ByteArrayUtils.concatAll(id, cmd));
            }
        }
        return cmdList;
    }

    public byte[] createCanTime(int spindle) {
        byte[] data = new byte[10];
        data[1] = (byte) (spindle + 1);
        data[2] = 0x1f;
        data[3] = 1;
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        data[4] = (byte) (year & 0xff);
        data[5] = (byte) ((year >> 8) & 0xff);
        data[6] = (byte) day;
        data[7] = (byte) month;
        return data;
    }

    public ArrayList<byte[]> createCanCmd(int index) {
        String mainKey = "" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        // cmd is just contain index
        byte[] cmd = Utility.ByteArrayUtils.short2ByteArray((short) ((index << 8) | 0x01));
        // after loop cmd is concated with value
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            int type = typeList.get(i);
            int subValue = Integer.valueOf(sp.getString(key, "0"));
            switch (type) {
                case Byte.SIZE: {
                    byte[] data = new byte[]{(byte) (subValue & 0xff)};
                    cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                }
                break;
                case Short.SIZE: {
                    byte[] data = Utility.ByteArrayUtils.short2SwapByteArray((short) subValue);
                    cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                }
                break;
                case Integer.SIZE: {
                    byte[] data = Utility.ByteArrayUtils.int2ByteArray(subValue);
                    cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                }
                break;
            }
        }
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();
        int groupCount = GroupMap.getInstance().getGroupCount();
        for (int group = 1; group <= groupCount; ++group) {
            List<Integer> spindleList = GroupMap.getInstance().getSpindleList(group);
            for (int spindle : spindleList) {
                byte[] id = Utility.ByteArrayUtils.short2ByteArray((short) (spindle + 1));
                cmdList.add(Utility.ByteArrayUtils.concatAll(id, cmd));
            }
        }
        return cmdList;
    }


    public ArrayList<byte[]> createCanCmd(int group, int index, int subIndex, String value) {
        String mainKey = group + "-" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        // cmd is just contain index
        byte[] cmd = Utility.ByteArrayUtils.short2ByteArray((short) ((index << 8) | 0x01));
        // after loop cmd is concated with value
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            String key1 = index + "-" + i;
            int type = typeList.get(i);
            String defv = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key1).getDefaultValue();
            int subValue = Integer.valueOf(sp.getString(key, defv));
            switch (type) {
                case Byte.SIZE: {
                    if (i != subIndex) {
                        byte[] data = new byte[]{(byte) (subValue & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = new byte[]{(byte) (Integer.valueOf(value) & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Short.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray((short) subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray(Short.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Integer.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(Integer.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
            }
        }
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();

        List<Integer> spindleList = GroupMap.getInstance().getSpindleList(group);
        for (int spindle : spindleList) {
            byte[] id = Utility.ByteArrayUtils.short2ByteArray((short) (spindle + 1));
            cmdList.add(Utility.ByteArrayUtils.concatAll(id, cmd));
        }
        return cmdList;
    }

    public ArrayList<byte[]> createCanCmd24(int group, int index, int subIndex, String value) {
        String mainKey = group + "-" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        // cmd is just contain index
        byte[] cmd = Utility.ByteArrayUtils.short2ByteArray((short) ((index << 8) | 0x01));
        // after loop cmd is concated with value
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            String key1 = index + "-" + i;
            int type = typeList.get(i);
            int subValue;
            String defv = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key1).getDefaultValue();

            if (i == 0) {
                float pkg_len = Float.parseFloat(sp.getString(key, defv));
                float temp_metre = CommonVar.getMetreFromLength(pkg_len);
                int pkg_len_metre = Math.round(temp_metre);

                float slip_coef = Float
                        .parseFloat(getValue(group, MachineParameterSharedPreference.SPINDLE_SLIP));
                float temp_pulse = pkg_len_metre * CommonVar.LENGTH_TO_PULSE_FACTOR * slip_coef;
                subValue = Math.round(temp_pulse);

            } else if (i == 1) {
                subValue = Integer.valueOf(sp.getString(key, defv));
            } else {
                subValue = Float.valueOf(sp.getString(key, defv)).intValue();
            }

            switch (type) {
                case Byte.SIZE: {
                    if (i != subIndex) {
                        byte[] data = new byte[]{(byte) (subValue & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = new byte[]{(byte) (Integer.valueOf(value) & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Short.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray((short) subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray(Short.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Integer.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(Integer.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
            }
        }
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();

        List<Integer> spindleList = GroupMap.getInstance().getSpindleList(group);
        for (int spindle : spindleList) {
            byte[] id = Utility.ByteArrayUtils.short2ByteArray((short) (spindle + 1));
            cmdList.add(Utility.ByteArrayUtils.concatAll(id, cmd));
        }
        return cmdList;
    }

    public ArrayList<byte[]> createCanCmd28(int group, int index, int subIndex, String value) {
        String mainKey = group + "-" + index;
        ArrayList<Integer> typeList = ParameterMap.instance().getTypeList(index);
        // cmd is just contain index
        byte[] cmd = Utility.ByteArrayUtils.short2ByteArray((short) ((index << 8) | 0x01));
        // after loop cmd is concated with value
        for (int i = 0; i < typeList.size(); ++i) {
            String key = mainKey + "-" + i;
            String key1 = index + "-" + i;
            int type = typeList.get(i);
            int subValue;

            if (i == 1) {
                String s_yarn_cnt = getValue(group, 0x28, 1);
                float temp = Float.parseFloat(s_yarn_cnt);
                subValue = (int) temp;
            } else {
                String defv = SmaroWindingMachineDataAccessRules.getInstance().getDataAccessRule(key1).getDefaultValue();
                subValue = Integer.valueOf(sp.getString(key, defv));
            }

            switch (type) {
                case Byte.SIZE: {
                    if (i != subIndex) {
                        byte[] data = new byte[]{(byte) (subValue & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = new byte[]{(byte) (Integer.valueOf(value) & 0xff)};
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Short.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray((short) subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray(Short.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
                case Integer.SIZE: {
                    if (i != subIndex) {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(subValue);
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    } else {
                        byte[] data = Utility.ByteArrayUtils.int2ByteArray(Integer.valueOf(value));
                        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
                    }
                }
                break;
            }
        }
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();

        List<Integer> spindleList = GroupMap.getInstance().getSpindleList(group);
        for (int spindle : spindleList) {
            byte[] id = Utility.ByteArrayUtils.short2ByteArray((short) (spindle + 1));
            cmdList.add(Utility.ByteArrayUtils.concatAll(id, cmd));
        }
        return cmdList;
    }

    public ArrayList<byte[]> createCanCmd75(int index, int subIndex, int dataDB, String value) {//0x7501
        byte[] cmd = Utility.ByteArrayUtils.short2ByteArray((short) ((index << 8) | 0x01));
        byte[] data1 = Utility.ByteArrayUtils.short2SwapByteArray((short) dataDB);
        cmd = Utility.ByteArrayUtils.concatAll(cmd, data1);
        byte[] data = Utility.ByteArrayUtils.short2SwapByteArray(Short.valueOf(value));
        cmd = Utility.ByteArrayUtils.concatAll(cmd, data);
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();
        byte[] id = {0x00, (byte) 0xA0};//Utility.ByteArrayUtils.short2ByteArray((short) (0xA0 & 0xff));
        cmdList.add(Utility.ByteArrayUtils.concatAll(id, cmd));
        return cmdList;

    }

    public ArrayList<byte[]> getBroadcast() {
        ArrayList<byte[]> list = new ArrayList<byte[]>();
        list.add(new byte[]{0x00, 0x55, (byte) 0xAA, 0x01, 0, 0, 0, 0, 0, 0});
        return list;
    }

    public ArrayList<byte[]> releaseBroadcast() {
        ArrayList<byte[]> list = new ArrayList<byte[]>();
        list.add(new byte[]{0x00, 0x55, 0x55, 0x01, 0, 0, 0, 0, 0, 0});
        return list;
    }

    // +自动化项目配重功能

    public static final String LENGTH_ADJUST_RATIO = "length_adjust_ratio";
    public static final String LENGTH_ADJUST_ACCUMULATIVE_RATIO = "length_adjust_accumulative_ratio";
    public static final String LENGTH_ADJUST_TIME = "length_adjust_time";

    public void setLengthAdjustRatio(int diffRatio) {
        sp.edit().putInt(LENGTH_ADJUST_RATIO, diffRatio).commit();
    }

    public int getLengthAdjustRatio() {
        return sp.getInt(LENGTH_ADJUST_RATIO, 0);
    }

    public void setLengthAdjustAccumulativeRatio(int diffRatio) {
        if (diffRatio > 200)
            diffRatio = 200;
        if (diffRatio < -200)
            diffRatio = -200;
        float ratio = getLengthAccumulativeRatio();
        ratio = ratio * (10000 + diffRatio) / 10000;
        sp.edit().putFloat(LENGTH_ADJUST_ACCUMULATIVE_RATIO, ratio).commit();
    }

    public float getLengthAccumulativeRatio() {
        return sp.getFloat(LENGTH_ADJUST_ACCUMULATIVE_RATIO, 1);
    }

    public void setLengthAdjustRatioTime(String time) {
        sp.edit().putString(LENGTH_ADJUST_TIME, time).commit();
    }

    public String getLengthDiffRatioTime() {
        return sp.getString(LENGTH_ADJUST_TIME, "");
    }
    // -自动化项目配重功能

    public void registerMachineParaChangeListener(OnSharedPreferenceChangeListener listener) {
        sp.registerOnSharedPreferenceChangeListener(listener);
    }

    public void unregisterMachineParaChangeListener(OnSharedPreferenceChangeListener listener) {
        sp.unregisterOnSharedPreferenceChangeListener(listener);
    }

}
